﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AgileToDo.Common;
using Data;
using Entity;

namespace AgileToDo
{
    /// <summary>
    /// UPItem.xaml 的交互逻辑
    /// </summary>
    public partial class UPItem : UserControl
    {
        public UPItem()
        {
            InitializeComponent();
        }

        private bool _isEdit;
        public bool IsEdit
        {
            get { return _isEdit; }
            set
            {
                _isEdit = value;
                if (value)
                {
                    this.tbx.Visibility = Visibility.Visible;
                    this.label.Visibility = Visibility.Collapsed;
                    this.tbx.Focus();
                }
                else
                {
                   // MessageBox.
                    Debug.WriteLine("PROJECT CLOSE EDIT");
                    this.tbx.Visibility = Visibility.Collapsed;
                    this.label.Visibility = Visibility.Visible;
                    DbManager.ToDoContext.SaveChanges();
                   
                }
            }
        }

        private bool _selected;
        public bool Selected 
        { 
            get { return _selected; }
            set
            {
                var bc = new BrushConverter();
                if (value)
                {
                    this.border.Background =ColorHelper.SelectPIBackColor;
                }
                else
                {
                    this.border.Background = ColorHelper.DefaultPIBackColor;
                }
                this._selected = value;
            }
        }
        public event EventHandler Click;

        private void border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Selected = true;
          
            if(Click!=null)
            {
                Click(this, e);
            }

        }

        private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            this.IsEdit = true;
        }

        private void tbx_LostFocus(object sender, RoutedEventArgs e)
        {
            IsEdit = false;
        }

        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;

        }

        private void imgClose_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var pj = this.DataContext as Project;

            if(pj.ToDos!=null&&pj.ToDos.Count>0)
            {
                UMessageBox.Show("提示", "该类别下含有待办不能删除!");
               
            }
            else
            {
                var result = UMessageBox.Show("删除确认", "是否确定删除该项目？",MessageBoxButton.YesNo);
                if (result.HasValue && result.Value)
                {
                    var st = this.Parent as StackPanel;
                    st.Children.Remove(this);
                    DbManager.ToDoContext.Projects.Remove(pj);
                    DbManager.ToDoContext.SaveChanges();
                }
            }
          

        }
    }
}
